using MultiAgentsShared;
using static MultiAgentsShared.Debug;

namespace MultiAgentsClient.Presentation;

public partial record MessagePopupModel
{
    private readonly IEventBus eventBus;

    public IState<bool> IsPopupOpen => State.Value(this, () => false);
    public IState<string> MessageContent => State.Value(this, () => "");

    public MessagePopupModel()
    {
        eventBus = EventBus.Instance;

        eventBus.Subscribe<DisplaySystemMessageCommand>(async cmd => { await DisplaySystemMessageCommandHandler(cmd); });
    }

    public async Task CloseMessagePopup()
    {
        await IsPopupOpen.SetAsync(false);
        await MessageContent.SetAsync("");
    }

    private async Task DisplaySystemMessageCommandHandler(DisplaySystemMessageCommand cmd)
    {
        await IsPopupOpen.SetAsync(true);
        await MessageContent.SetAsync(cmd.Message);

        await Task.Delay(2500);
        await CloseMessagePopup();
    }
}
